﻿using System;

namespace YEA.Utilities.Notifications
{
    public class Notifier : INotify , IDisposable
    {
        public const string NOTIFICATION_KEY_NAME = "notificationPublisherName";
        private Client server;
        public Notifier()
        {
            string publisherName = System.Configuration.ConfigurationManager.AppSettings[NOTIFICATION_KEY_NAME];
            if (publisherName != null)
                Initialize(publisherName);
            else
                throw new ArgumentException(string.Format("The notification utility needs to know the endpoint name to send notifications to, please enter it in either the app config with key '{0}' or pass the name in the constructor.", NOTIFICATION_KEY_NAME));    
        }
        public Notifier(string publisherName)
        {
            Initialize(publisherName);
        }

        private void Initialize(string publisherName)
        {
            server = new Client(publisherName);
        }
        ~Notifier()
        {
            Dispose(false);
        }
        public void Transmit(Notification message)
        {
            server.Send(message);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        private void Dispose(bool disposing)
        {
            if (disposing)
                server.Dispose();
            server = null;
        }
    }
}
